<?php

namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\MemberModel;
use think\Db;

class MemberController extends AdminBaseController{
    //会员管理
    public function index(){
        return $this->fetch();
    }
    //会员列表
    public function solist(){
        //查询手机号，昵称
        $request = input('request.');
        $keywordComplex = [];
        if(!empty($request['user_login'])){
            $keyword = $request['user_login'];
            $keywordComplex['c.nick_name|c.mobile'] = ['like',"%$keyword%"];
        }
        //查询会员，按照时间段查询
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c.c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c.c_time ) ) <= "'.$request['end_time'].'"';
        }
        $data = new MemberModel();
        $list = $data->indexlist($keywordComplex,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign("list",$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //判断会员状态 冻结
    public function ban(){
        $id = $this->request->param('id');
        if(!empty($id)){
            $result = Db::name("member")->where(["id" => $id])->setField('status', '0');
            if($result !== false){
                $this->success("会员冻结成功！", url("member/solist"));
            }else{
                $this->error('会员停用失败！');
            }
        }else{
            $this->error('数据传入失败！');
        }
    }
    //判断会员恢复
    public function cancelBan()
    {
        $id = $this->request->param('id');
        if (!empty($id)) {
            $result = Db::name('member')->where(["id" => $id])->setField('status', '1');
            if ($result !== false) {
                $this->success("会员恢复成功！", url("member/solist"));
            } else {
                $this->error('会员启用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
    //添加会员
    public function add(){
        return $this->fetch();
    }
    //添加会员数据
    public function addpost(){
        if($this->request->isPost()){
            $post = $this->request->post();
            $data = new MemberModel();
            $result = $data->addpo($post);
            if($result){
                $this->success("添加成功","blog/Member/solist");
            }else{
                $this->error("添加失败","blog/Member/add");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    /*public function memdelete(){
        //获取删除的m_guid,获取唯一标识
        $m_guid = $this->request->param('nav_id');
        $data = new MemberModel();
        $result = $data->MemeberDelete($m_guid);
        if($result){
            $this->success("删除成功","blog/Member/solist");
        }else{
            $this->error("删除失败","blog/Member/solist");
        }
    }*/
    //获取会员历史信息
    public function memuplist(){
        $m_guid = $this->request->param("nav_id");
        $data = new MemberModel();
        $list = $data->MemberHistory($m_guid);
        $this->assign('list',$list);
        $this->assign('m_guid',$m_guid);
        return $this->fetch();
    }
    //修改会员信息
    public function mempost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $from = new MemberModel();
            $result = $from->MemberUpdate($data);
            if($result){
                $this->success("修改成功","blog/Member/solist");
            }else{
                $this->error("修改失败","blog/Member/mempost");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //查询会员余额变更记录
    public function moneylog(){
        //按照时间查询
        $request = $this->request->param();
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c_time ) ) <= "'.$request['end_time'].'"';
        }
        //获取唯一标识
        $m_guid = $request["m_guid"];
        $data = new MemberModel();
        $list = $data->MemberMoneyLog($m_guid,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign('m_guid',$m_guid);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //会员等级变更记录
    public function paylevelog(){
        $request = $this->request->param();
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c_time ) ) <= "'.$request['end_time'].'"';
        }
        $m_guid = $request["m_guid"];
        $data = new MemberModel();
        $list = $data->MemberPayleveLog($m_guid,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign('m_guid',$m_guid);
        $this->assign('page',$list);
        return $this->fetch();
    }
    //会员积分变更记录
    public function integralog(){
        $request = $this->request->param();
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( c_time ) ) <= "'.$request['end_time'].'"';
        }
        $m_guid = $request["m_guid"];
        $data = new MemberModel();
        $list = $data->MemberIntegraLog($m_guid,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign('m_guid',$m_guid);
        $this->assign('page',$list);
        return $this->fetch();
    }
    /*****************************会员卡********************************** */
    //会员卡列表
    public function mcardlist(){
        //查询手机号，昵称
        $request = input('request.');
        $keywordComplex = [];
        if(!empty($request['user_login'])){
            $keyword = $request['user_login'];
            $keywordComplex['a.mobile|b.real_name'] = ['like',"%$keyword%"];
        }
        //查询会员，按照时间段查询
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.c_time ) ) <= "'.$request['end_time'].'"';
        }
        $data = new MemberModel();
        $list = $data->MemberMcard($keywordComplex,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign("list",$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //判断会员卡状态 冻结
    public function cardban(){
        $id = $this->request->param('id');
        if(!empty($id)){
            $data = new MemberModel();
            $result = $data->MemberCardban($id);
            if($result !== false){
                $this->success("会员冻结成功！", url("member/mcardlist"));
            }else{
                $this->error('会员停用失败！');
            }
        }else{
            $this->error('数据传入失败！');
        }
    }
    //判断会员卡恢复
    public function cardcancelBan()
    {
        $id = $this->request->param('id');
        if (!empty($id)) {
            $data = new MemberModel();
            $result = $data->MemberCardcancelBan($id);
            if ($result !== false) {
                $this->success("会员恢复成功！", url("member/mcardlist"));
            } else {
                $this->error('会员启用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
    //添加会员卡
    public function cardadd(){
        return $this->fetch();
    }
    public function cardpost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $ergodic = new MemberModel();
            $result = $ergodic->MemberCardPost($data);
            if($result){
                $this->success("添加成功","blog/Member/mcardlist");
            }else{
                $this->error("添加失败","blog/Member/cardadd");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //会员卡修改
    public function cardupdate(){
        $m_guid = $this->request->param("nav_id");
        $data = new MemberModel();
        $list = $data->MemberCardHistory($m_guid);
        $this->assign('m_guid',$m_guid);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //会员卡修改数据
    public function cardupdatepost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $ergodic = new MemberModel();
            $result = $ergodic->MemberCardUpdatePost($data);
            if($result){
                $this->success("修改成功","blog/Member/mcardlist");
            }else{
                $this->error("修改失败","blog/Member/cardadd");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //查询会员拥有的会员卡
    public function ownmebcard(){
        $m_guid = $this->request->param("m_guid");
        $ergodic = new MemberModel();
        $list = $ergodic->MemberOwnmebCard($m_guid);
        $this->assign('list',$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //会员充值查询记录
    public function memberchangelog(){
        //查询手机号，昵称
        $request = input('request.');
        $keywordComplex = [];
        if(!empty($request['user_login'])){
            $keyword = $request['user_login'];
            $keywordComplex['c.nick_name|c.mobile'] = ['like',"%$keyword%"];
        }
        //查询会员，按照时间段查询
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.c_time ) ) <= "'.$request['end_time'].'"';
        }
        $result = new MemberModel();
        $list = $result->MemberChangeLoging($keywordComplex,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    /**************************会员等级****************************************/
    //等级列表
    public function memberlevelist(){
        //查询代理手机号
        $request = input('request.');
        $keywordComplex = [];
        if(!empty($request['user_login'])){
            $keyword = $request['user_login'];
            $keywordComplex['a.mobile'] = ['like',"%$keyword%"];
        }
        //查询会员，按照时间段查询
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.c_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.c_time ) ) <= "'.$request['end_time'].'"';
        }
        $ergodic = new MemberModel();
        $list = $ergodic->MemberLevelc($keywordComplex,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //判断会员等级状态 冻结
    public function levelban(){
        $id = $this->request->param('id');
        if(!empty($id)){
            $data = new MemberModel();
            $result = $data->MemberLevelban($id);
            if($result !== false){
                $this->success("会员等级冻结成功！", url("member/memberlevelist"));
            }else{
                $this->error('会员等级停用失败！');
            }
        }else{
            $this->error('数据传入失败！');
        }
    }
    //判断会员等级恢复
    public function levelcancelBan()
    {
        $id = $this->request->param('id');
        if (!empty($id)) {
            $data = new MemberModel();
            $result = $data->MemberLevelcancelBan($id);
            if ($result !== false) {
                $this->success("会员等级恢复成功！", url("member/memberlevelist"));
            } else {
                $this->error('会员等级启用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
    //会员添加等级
    public function memberleveladdlist(){
        return $this->fetch();
    }
    //接收会员等级添加数据
    public function memberlevelpost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $ergodic = new MemberModel();
            $result = $ergodic->MemberLevelPost($data);
            if($result){
                $this->success("添加成功","blog/Member/memberlevelist");
            }else{
                $this->error("添加失败","blog/Member/memberleveladdlist");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //会员等级修改
    public function memberlevelupdate(){
        $m_guid = $this->request->param("nav_id");
        $data = new MemberModel();
        $list = $data->MemberLevelHistory($m_guid);
        $this->assign('m_guid',$m_guid);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //接收会员等级修改信息数据
    public function memberlevelhistorypost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $ergodic = new MemberModel();
            $result = $ergodic->MemberLevelUpdate($data);
            if($result){
                $this->success("修改成功","blog/Member/memberlevelist");
            }else{
                $this->error("添加失败","blog/Member/memberlevelupdate");
            }
        }else{
            $this->error(lang('ERROR'));
        }
    }
    //删除会员等级
    public function memberleveldelete(){
        //获取删除的m_guid,获取唯一标识
        $m_guid = $this->request->param('nav_id');
        $data = new MemberModel();
        $result = $data->MemberLevelDelete($m_guid);
        if($result){
            $this->success("删除成功","blog/Member/memberlevelist");
        }else{
            $this->error("删除失败","blog/Member/memberlevelist");
        }
    }
    /****************************预约功能设置***************************************/
    //预约管理列表
    public function appointmentindex(){
        //查询代理手机号
        $request = input('request.');
        $keywordComplex = [];
        if(!empty($request['user_login'])){
            $keyword = $request['user_login'];
            $keywordComplex['a.mobile'] = ['like',"%$keyword%"];
        }
        $ergodic = new MemberModel();
        $list = $ergodic->MemberAppointment($keywordComplex);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
        //判断预约状态 不可以
    public function appointmentban(){
        $id = $this->request->param('id');
        if(!empty($id)){
            $data = new MemberModel();
            $result = $data->MemberAppoinmentban($id);
            if($result !== false){
                $this->success("预约状态关闭成功！", url("member/appointmentindex"));
            }else{
                $this->error('预约状态关闭失败！');
            }
        }else{
            $this->error('数据传入失败！');
        }
    }
    //判断预约可以
    public function appointmentBango()
    {
        $id = $this->request->param('id');
        if (!empty($id)) {
            $data = new MemberModel();
            $result = $data->MemberAppoinmentCarban($id);
            if ($result !== false) {
                $this->success("预约状态开启成功！", url("member/appointmentindex"));
            } else {
                $this->error('预约状态开启失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
    //预约添加
   public function  appointmentaddlist(){
       return $this->fetch();
   }
   //添加预约接收数据
   public function appointmentaddpost(){
       if($this->request->isPost()){
           $data = $this->request->post();
           $ergodic = new MemberModel();
           $result = $ergodic->MemberAppoPostAdd($data);
           if($result){
               $this->success("预约添加成功!", "blog/Member/appointmentindex");
           }else{
               $this->error("预约添加失败!","blog/Member/appointmentaddlist");
           }
       }else{
           $this->error(lang('ERROR'));
       }
   }
   //修改预约
   public function appointmentupdatelist(){
       $m_guid = $this->request->param("nav_id");
       $data = new MemberModel();
       $list = $data->MemberAppoinmentHistory($m_guid);
       $this->assign('m_guid',$m_guid);
       $this->assign('list',$list);
       return $this->fetch();
   }
   //修改数据提交
   public function appointmentupdatepost(){
       if($this->request->isPost()){
           $data = $this->request->post();
           $ergodic = new MemberModel();
           $result = $ergodic->MemberAppoinmentUpdatePost($data);
           if($result){
               $this->success("预约修改成功!", "blog/Member/appointmentindex");
           }else{
               $this->error("预约修改失败!","blog/Member/appointmentupdatelist");
           }
       }else{
           $this->error(lang('ERROR'));
       }
   }
   //删除预约
   public function appointmentdelete(){
        //获取删除的m_guid,获取唯一标识
        $m_guid = $this->request->param('nav_id');
        $data = new MemberModel();
        $result = $data->AppointmentDelete($m_guid);
        if($result){
            $this->success("删除成功","blog/Member/appointmentindex");
        }else{
            $this->error("删除失败","blog/Member/appointmentindex");
        }
    }
    //会员预约订单记录查询
    public function appintmentorder(){
        //查询手机号，昵称
        $request = input('request.');
        $keywordComplex = [];
        if(!empty($request['user_login'])){
            $keyword = $request['user_login'];
            $keywordComplex['a.mobile'] = ['like',"%$keyword%"];
        }
        //查询会员，按照时间段查询
        $comeon = "1=1";
        if(!empty($request['start_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.appone_time ) ) >= "'.$request['start_time'].'"';
        }
        if(!empty($request['end_time'])){
            $comeon .= ' and DATE( FROM_UNIXTIME( d.appone_time ) ) <= "'.$request['end_time'].'"';
        }
        $ergodic = new MemberModel();
        $list = $ergodic->AppointmentLog($keywordComplex,$comeon);
        //查询保持分页不变
        $list->appends($request);
        $this->assign('list',$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //判断会员订单是否来店 没有来店
    public function appointmentforban(){
        $id = $this->request->param('id');
        if(!empty($id)){
            $data = new MemberModel();
            $result = $data->MemberAppoinmentLogban($id);
            if($result !== false){
                $this->success("预约记录设置未来店成功！", url("member/appintmentorder"));
            }else{
                $this->error('预约记录设置未来店失败！');
            }
        }else{
            $this->error('数据传入失败！');
        }
    }
    //判断会员订单是否来店 来店
    public function appointmentforbango()
    {
        $id = $this->request->param('id');
        if (!empty($id)) {
            $data = new MemberModel();
            $result = $data->AppoCarLogban($id);
            if ($result !== false) {
                $this->success("预约记录设置已来店成功！", url("member/appintmentorder"));
            } else {
                $this->error('预约记录设置已来店失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
}
